package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;

/**
 *  GogNewsAnalyse  贵州金黔在线新闻解析类
 *  xiaonie 2012-11-23
 */
public class GogNewsAnalyse implements AnalyseNews{
	private final String[] regexs={
			"http://.*.gog.cn/system/[0-9]+/[0-9]+/[0-9]+/[0-9]+.shtml",
			"http://.*.gog.cn/system/[0-9]+/[0-9]+/[0-9]+/[0-9]+.shtml",
			"http://[a-z]+.gog.com.cn/system/[0-9]+/[0-9]+/[0-9]+/[0-9]+.shtml"

	};

	
	public boolean isDetailPage(String url) {
		if(url.startsWith("http://pop.gog.com.cn/")){
			return false;
		}
		return RegexUtils.matchAnyIgnoreCase(url, regexs);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		news.setUrl(url);
		title=doc.select("td.title").text();
		if (StringUtils.isBlank(title)) {
			title=doc.select("td.gog_title").text();
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select("h1").text();
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select(".articletitle_p22").text();
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select(".gog_title").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("td.contents"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("td.gog_content"));
			if (StringUtils.isBlank(content)) {
				content=HtmlCleaner.getContentHtml(url,doc.select("div.nph_cnt"));
				if (StringUtils.isBlank(content)) {
					content=HtmlCleaner.getContentHtml(url,doc.select("div.bd"));
				}
			}
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select(".p16 p"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select(".gog_content"));
		}

		if(date==null){
			date=DateUtils.matchDate(doc.select("div.nph_set_title").text());
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.info").text());
		}
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		news.setContent(content);
		news.setTitle(title);
		news.setClickNum(null);
		news.setCommentNum(null);
		news.setDate(date);
		news.setType(1);

		String author= JSoupUtils.matchAuthor(doc, "来源:,作者：");

		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		GogNewsAnalyse a=new GogNewsAnalyse();
		String url="http://ent.gog.com.cn/system/2013/09/12/012674055.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(a.isDetailPage(url));
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
